<template>
  <div :class="$style.wrap">
    <Button type="primary" @click="openHandle">变更分配坐席</Button>
    <el-dialog :visible.sync="visible" title="变更分配坐席" :close-on-click-modal="false" @close="resetForm">
      <el-form ref="form" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item prop="handler" label="分配坐席变更为">
          <StaffSearch v-model="formData.handler" :name.sync="formData.handlerName" placeholder="请选择分配坐席" clearable></StaffSearch>
        </el-form-item>
        <el-form-item>
          <Button @click="cancelHandle">取消</Button>
          <Button type="primary" @click="confirmHandle">确定</Button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { postChangeSeats } from '@/api/guestCare'
export default {
  name: 'SeatChange',
  components: {
  },
  props: {
    selected: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      formData: {
        handler: '',
        handlerName: ''
      },
      formRules: {
        handler: [{ required: true, message: '请选择分配坐席', trigger: 'change' }]
      }
    }
  },
  methods: {
    cancelHandle() {
      this.visible = false
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = this.selected.map(e => {
          return {
            id: e.id,
            handler: this.formData.handler,
            handlerName: this.formData.handlerName
          }
        })
        return postChangeSeats(data).then(res => {
          this.$emit('refresh')
          this.visible = false
          this.$message.success('分配坐席成功！')
        })
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    },
    openHandle() {
      if (this.selected.length > 0) {
        this.visible = true
      } else {
        this.$message.info('请选择要变更分配坐席的数据！')
      }
    }
  }
}
</script>
<style lang='scss' module>
.wrap{
display: inline-block;
}
</style>
